﻿using System;
using System.Windows;
using System.Windows.Media;
using Mesuro.Common;
using Mesuro.Graphing;
using Mesuro.Graphing.Utilities;
using Vector = Mesuro.Common.Vector;
using System.Windows.Controls;
using Mesuro.Graphing.PropertiesControls;
using Mesuro.Graphing.Plots;

namespace TestApp {

	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window {
		public MainWindow() {
			Plots2D.Load();
			Plots3D.Load();
			//Create the interface from XAML
			InitializeComponent();

			//MAYAVI - graph types
			//SVN should work this evening [20111209]
			//Σq parser for VR
			//contour Plots
			//3D Plots

			//Create Series to hold data [this replaces the database for testing purposes]
			AlphaTestOnlyMutableSeries sin = new AlphaTestOnlyMutableSeries( "θ", "j", "sin(θ)" );
			AlphaTestOnlyMutableSeries sinp = new AlphaTestOnlyMutableSeries( "θ", "r" );
			AlphaTestOnlyMutableSeries Z = new AlphaTestOnlyMutableSeries( "Resistance", "Reactance" );
			AlphaTestOnlyMutableSeries Y = new AlphaTestOnlyMutableSeries( "Conductance", "Susceptance" );
			AlphaTestOnlyMutableSeries rho = new AlphaTestOnlyMutableSeries( "Re(Γ)", "Im(Γ)" );

			#region Create Test Data
			for (double x = -1; x <= 1; x += 1d / 24) {
				double th = Math.PI * x;
				for (double j = -2; j <= 2; j += 2d / 24)
					sin.AddRecord( new Vector( Math.Round( x, 8 ), j, Math.Sin( j * x * 2 * Math.PI ) ) );
				sinp.AddRecord( new Vector( Math.Round( th, 5 ), Math.Sin( 2 * th ) ) );//Sin(2θ), θ ε [-π,π] => 2θ ε [-2π,2π]
				rho.AddRecord( (Vector)(0.8 + 0.5 * Math.Cos( x * Math.PI ) + Complex.I * (-0.6 + 0.5 * Math.Sin( x * Math.PI ))) );
				Z.AddRecord( (Vector)SmithAxes.ZFromRho( (0.5 + 0.25 * Math.Cos( th ) + Complex.I * 0.3 * Math.Sin( th )) ) );
				Y.AddRecord( (Vector)SmithAxes.YFromRho( (0.1 + 0.4 * Math.Cos( th ) - Complex.I * (0.6 + 0.6 * Math.Sin( th ))) ) );
			}
			#endregion

			//Add seriesDesc to graphs
			Plot cntPlot = new Plot( sin );
			//cntPlot.RenderType = Plots3D.PLOT_ChromaticMap;
			xyGraph.AddPlot( cntPlot );

			//Plot cldPlot = new Plot( sin );
			//cldPlot.RenderType = Plots2D.PLOT_PointCloud;
			////xyGraph.AddPlot( cldPlot );

			//Plot rhoPlot = new Plot( rho );
			//MultiAxes.SetAxisSet( rhoPlot, 0 );
			////xyGraph.AddPlot( rhoPlot );

			//smGraph.AddPlot( new Plot( rho ) );

			////smGraph.AddPlot( new Plot( sin ) );

			//Plot zPlot = new Plot( Z );
			//MultiAxes.SetAxisSet( zPlot, SmithAxes.AXES_IMPEDANCE );
			//smGraph.AddPlot( zPlot );

			//Plot yPlot = new Plot( Y );
			//MultiAxes.SetAxisSet( yPlot, SmithAxes.AXES_ADMITTANCE );
			//smGraph.AddPlot( yPlot );

			////tbGraph.AddPlot( new SeriesDesc( zRho ) );

			//plGraph.AddPlot( new Plot( sinp ) );

		}

		private void ShowGraphPropertiesContextHandler( object sender, RoutedEventArgs e ) {
			UIElement placementTarget = ((ContextMenu)((MenuItem)sender).Parent).PlacementTarget;
			
			while (placementTarget != null && !(placementTarget is Graph))
				placementTarget = (UIElement)VisualTreeHelper.GetParent( placementTarget );
			
			if (placementTarget == null) return;
			
			Graph graph = (Graph)placementTarget;
			GraphPropertiesDialog gpd = new GraphPropertiesDialog( graph );
			gpd.Show();
		}

	}
}
